package SV_STRUTS_STATIC;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;


import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Example extends ActionForm {


        private String birthdayString;
        protected Date birthday;
        private static SimpleDateFormat dateConvertor;
        public ActionErrors validate(ActionMapping map,
                                     HttpServletRequest req) {
            ActionErrors errors = new ActionErrors();
            if (dateConvertor == null) {
                dateConvertor = new SimpleDateFormat(req
                        .getParameter("date.format"));
            }
            try {
                birthday = dateConvertor.parse(birthdayString);
            } catch (ParseException e) {
                errors.add(ActionErrors.GLOBAL_MESSAGE,
                        new ActionMessage("Bad date"));
            }
            return super.validate(map, req);
        }
        public String getBirthday() {
            return birthdayString;
        }
        public void setBirthday(String birthday) {
            this.birthdayString = birthday;
        }

}
